package com.example.studyktdemo.adapter

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.studyktdemo.bean.FocusOnBean
import com.example.studyktdemo.bean.Item
import com.example.studyktdemo.databinding.ItemFocusOnBinding

class FocusOnAdapter : RecyclerView.Adapter<FocusOnAdapter.InnerHolder>() {

    private var focusOnData: FocusOnBean? = null

    class InnerHolder(var itemBinding: ItemFocusOnBinding) : RecyclerView.ViewHolder(itemBinding.root) {
        fun bind(holder: InnerHolder, data: Item) {
            itemBinding.tvTitle.text = data.data.header.title
            itemBinding.tvDesc.text = data.data.header.description
            Glide.with(holder.itemView).load(data.data.header.icon)
                .circleCrop().into(itemBinding.ivAvatar)

            val linearLayoutManager = LinearLayoutManager(holder.itemView.context, LinearLayoutManager.HORIZONTAL, false)

            itemBinding.flRv.addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    outRect.right = 20
                }
            })
            itemBinding.flRv.layoutManager = linearLayoutManager
            itemBinding.flRv.adapter = FocusItemAdapter(data.data.itemList)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
        val binding = ItemFocusOnBinding.inflate(LayoutInflater.from(parent.context), parent, false)

        return InnerHolder(binding)
    }

    override fun onBindViewHolder(holder: InnerHolder, position: Int) {
        holder.bind(holder,focusOnData!!.itemList[position]);
        /*binding.tvTitle.text = focusOnData!!.itemList[position].data.header.title
        binding.tvDesc.text = focusOnData!!.itemList[position].data.header.description
        Glide.with(holder.itemView.context).load(focusOnData!!.itemList[position].data.header.icon)
            .circleCrop().into(binding.ivAvatar)*/
    }

    override fun getItemCount(): Int {
        if (focusOnData!=null){
            return focusOnData!!.count
        }
        return 0
    }

    fun setData(focusData: FocusOnBean?) {
        if (focusData!= null){
            focusOnData = focusData
        }
        notifyDataSetChanged()
    }
}